<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {
    public function add(){
        if(IS_POST){
            unset($_POST['password1']);
            if(!empty($_FILES['size'])){
                $avatar=$this->upload();
            }
            $group_access['group_id']=$_POST['group'];
            unset($_POST['group']);
            $_POST['avatar']=$avatar;
            $_POST['password']=md5($_POST['password']);
            $admin=D('admin');
            $result=$admin->add(I('post.'));
            $group_access['uid']=$result;
            $res=D('group_access')->add($group_access);
            if($result && $res){
            $this->success('新增成功', 'lists');die;
            } else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置
                $this->error('新增失败');}die;
        }
        $group=D('group')->select();
        $this->assign('group',$group);
        $this->display();
    }
    //列表
    public function lists(){
        $admin=D('admin');
        $list=$admin->select();
        $this->assign('list',$list);
        $this->display();
    }
    //编辑
    public function edit(){

        $id=i('get.id');
        if(IS_POST){
            $_POST['id']=$id;

            // $_POST['group_access']['uid']=$id;
            if(!empty($_FILES['file'])){
                $avatar=$this->upload();
            }
            $row=D('admin')->relation(true)->where("id=$id")->save(i('post.'));

            if($row>=0){
            $this->success('修改成功', U('admin/admin/lists'));die;
            } else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置
                $this->error('修改失败');}die;

        }
        $list=D('admin')->find(I('get.id'));
        $group_access=D('group_access')->where("uid=$id")->find();
        $group=D('group')->select();
        $this->assign('group',$group);
        $this->assign('list',$list);
        $this->assign('group_access',$group_access);
        $this->display();
    }
    //登录
    public function login(){
        if(IS_POST){
            //验证码
            $verify = new \Think\Verify();
            // if(!$verify->check(I('post.code'))){
            //     $this->error('验证码错误');die;
            // }else{
                $_POST['password']=md5($_POST['password']);
                unset( $_POST['code']);
                $result=D('admin')->relation(true)->where(i('post.'))->find();
                if($result){
                    $result['group_access']=explode(',',$result['group_access'][0]['rules']);

                    session('admin',$result);
                     // print_r($_SESSION);DIE;
                    $this->success('登录成功',U('./admin/index'));die;
                }else{
                    $this->error('帐号或密码错误');}die;

            //}
        }
        $this->display();
    }
    //删除头像
    public function delavatar(){
            $data=D('admin')->getByid(I('get.id'));
            $map['id']=$data['id'];
            $filed['avatar']='';
            $id=D('admin')->where($map)->save($filed);
            if(1==1){
                $file='Uploads/'.$data['avatar'];
                unlink($file);
                 $this->success('删除成功',U('admin/edit?id='.$data['id']));
            }else{
                $this->success('删除失败');
            }
    }
    public function loginout(){
        session('[destroy]');
        $this->success('注销成功',U('admin/login'));
    }

}